package com.rjwh.dingdong.client.activity.download;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.download.fragment.DownLoadListFragment;
import com.rjwh.dingdong.client.activity.download.fragment.DownloadLocalFramgent;
import com.rjwh.dingdong.client.activity.download.fragment.DownloadUpdateFragment;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.widget.PagerSlidingTabStrip;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：whrjwh-parent
 * 类描述：下载管理器
 * 创建人：wby
 * 创建时间：2016/6/23 14:30
 * 修改人：wby
 * 修改时间：2016/6/23 14:30
 * 修改备注：
 *
 */
public class DownloadMangerActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.pagelidingtab)
    PagerSlidingTabStrip pagelidingtab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    private ArrayList<BaseFragment> listFragment = new ArrayList<BaseFragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comm_tabviewpage);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        listFragment.add(DownLoadListFragment.newInstance());
        listFragment.add(DownloadLocalFramgent.newInstance());
        listFragment.add(DownloadUpdateFragment.newInstance());
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        pagelidingtab.setViewPager(viewpager);
        initTabsValue();
        initToolbarHelper();
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {

        toolbar.setTitle("");
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(R.string.download_manage_title);
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    /**
     * mPagerSlidingTabStrip默认值配置
     */
    private void initTabsValue() {

        // 底部游标颜色@color/blue_bg
        pagelidingtab.setIndicatorColor(getResources().getColor(R.color.blue_button_nor));
        pagelidingtab.setTabPaddingLeftRight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // tab的分割线颜色
        pagelidingtab.setDividerColor(Color.TRANSPARENT);
        // tab背景
        pagelidingtab.setBackgroundColor(getResources().getColor(R.color.white));
        // tab底线高度
        pagelidingtab.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 游标高度
        pagelidingtab.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        pagelidingtab.setSelectedTextColor(getResources().getColor(R.color.blue_button_nor));
        // 正常文字颜色
        pagelidingtab.setTextColor(Color.BLACK);
        pagelidingtab.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        pagelidingtab.setShouldExpand(true);

    }

    /* ***************FragmentPagerAdapter***************** */
    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"下载列表", "下载完成", "更新"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            // TODO Auto-generated method stub
            return super.isViewFromObject(view, object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            // super.destroyItem(container, position, object);
        }
    }
}
